package com.buu.evaluate.controller;

import com.buu.common.annotation.Log;
import com.buu.common.utils.PageUtils;
import com.buu.common.utils.Query;
import com.buu.common.utils.R;
import com.buu.evaluate.domain.EvaluateInfoDO;
import com.buu.evaluate.service.EvaluateInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评价指标管理
 */
@RequestMapping("/evaluate")
@Controller
public class EvaluateInfoController {

    @Autowired
    private EvaluateInfoService evaluateInfoService;

    @GetMapping("/teacher")
    String teacher(Model model) {
        return "evaluate/teacher/teacher";
    }

    @GetMapping("/student")
    String student(Model model) {
        return "evaluate/student/student";
    }

    @Log("添加教师评价单信息")
    @GetMapping("/teacher/add")
    String addTea(Model model) {
        return "evaluate/teacher/add";
    }

    @Log("添加学生评价单信息")
    @GetMapping("/student/add")
    String addStu(Model model) {
        return "evaluate/student/add";
    }

    @Log("保存评价单信息")
    @ResponseBody
    @PostMapping("/save")
    R save(EvaluateInfoDO evaluateInfoDO) {

        if (evaluateInfoService.save(evaluateInfoDO) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @GetMapping("/list")
    @ResponseBody
    PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<EvaluateInfoDO> eduList = evaluateInfoService.list(query);
        int total = eduList.size();
        PageUtils pageUtil = new PageUtils(eduList, total);
        return pageUtil;
    }

    @Log("编辑评价单信息")
    @GetMapping("/edit/{id}")
    String edit(Model model, @PathVariable("id") Long id) {
        EvaluateInfoDO evaluateInfoDO = evaluateInfoService.get(id);
        model.addAttribute("eva", evaluateInfoDO);
        return "evaluate/teacher/edit";
    }

    @Log("更新用户信息")
    @PostMapping("/update")
    @ResponseBody
    R update(EvaluateInfoDO evaluateInfoDO) {
        if(evaluateInfoService.update(evaluateInfoDO) > 0){
            return R.ok();
        }
        return R.error();
    }

    @Log("预览教师评价单")
    @GetMapping("/teacher/view")
    String viewTea(Model model) {
        List<EvaluateInfoDO> evaluateInfoDO = evaluateInfoService.list(new HashMap<>());
        model.addAttribute("eva", evaluateInfoDO);
        return "evaluate/teacher/view";
    }

    @Log("预览学生评价单")
    @GetMapping("/student/view")
    String viewStu(Model model) {
        List<EvaluateInfoDO> evaluateInfoDO = evaluateInfoService.list(new HashMap<>());
        model.addAttribute("eva", evaluateInfoDO);
        return "evaluate/student/view";
    }

    @GetMapping("/viewlist")
    @ResponseBody
    PageUtils viewList(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        List<EvaluateInfoDO> eduList = evaluateInfoService.list(params);
        int total = eduList.size();
        PageUtils pageUtil = new PageUtils(eduList, total);
        return pageUtil;
    }

    @Log("删除评价指标")
    @PostMapping("/remove")
    @ResponseBody
    Map<String, Boolean> remove(Long id) {
        Map<String, Boolean> rMap = new HashMap<>();
        rMap.put("success", evaluateInfoService.remove(id) > 0);
        return rMap;
    }


    @Log("批量评价指标")
    @PostMapping("/batchRemove")
    @ResponseBody
    R batchRemove(@RequestParam("ids[]") Long[] userIds) {

        List<Long> Ids = Arrays.asList(userIds);
        int r = evaluateInfoService.batchRemove(Ids);
        if (r > 0) {
            return R.ok();
        }
        ;
        return R.error();
    }
}
